package com.dgw.exceptions;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: spring-boot-web-restfulcrud
 * @description:
 * @author: Mr.Dai
 * @create: 2019-12-03 16:11
 **/
@RestControllerAdvice
public class UserExceptionHandler {
    @ExceptionHandler(UserNotFoundExits.class)
    public ModelAndView handleException(Exception e, HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        map.put("code","user.notexist");
        map.put("message","用户不存在");
        request.setAttribute("ext",map);
        //设置状态码 来响应不同的状态
        request.setAttribute("javax.servlet.error.status_code",500);
        return new ModelAndView("forward:/error");
    }
}
